package com.ttb.smshelper;

import java.util.ArrayList;

import com.ttb.database.Contact;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.AdapterView.OnItemLongClickListener;

public class privatebox extends Activity {
	EditText editName, editPhone;
	Button btnAdd;
	ArrayList<list_private> arrPrivate = new ArrayList<list_private>();
	ArrayList<Contact> contactList = new ArrayList<Contact>();
	ArrayAdapter<Contact> adapter = null;
	ListView lvprivatelist;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.manhinh_privatebox);
		doFormWidgets();
		getDefaultInfor();
		addEventFormWidgets();
		loadTabs();
	}

	private void getDefaultInfor() {
		editName.requestFocus();

	}

	private void doFormWidgets() {

		editName = (EditText) findViewById(R.id.editName);
		editPhone = (EditText) findViewById(R.id.editPhone);
		btnAdd = (Button) findViewById(R.id.btnAddContact);
		lvprivatelist = (ListView) findViewById(R.id.lvprivatelist);
		adapter = new ArrayAdapter<Contact>(this,
				android.R.layout.simple_list_item_1, contactList);
		lvprivatelist.setAdapter(adapter);

	}

	private void addEventFormWidgets() {

		btnAdd.setOnClickListener(new MyButtonEvent());
		lvprivatelist.setOnItemLongClickListener(new MyListViewEvent());

	}

	private class MyButtonEvent implements OnClickListener {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {

			case R.id.btnAddContact:
				processAddJob();
				break;
			}
		}

		private void processAddJob() {

			Log.d("click","BInh");
			String name = editName.getText() + "";
			String phone = editPhone.getText() + "";
			Contact contact = new Contact(name,phone);
			contactList.add(contact);
			adapter.notifyDataSetChanged();

			editName.setText("");
			editPhone.setText("");
			editName.requestFocus();
		}

	}

	private class MyListViewEvent implements OnItemLongClickListener {

		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
				int arg2, long arg3) {

			arrPrivate.remove(arg2);
			adapter.notifyDataSetChanged();
			return false;
		}

	}

	public void loadTabs() {

		final TabHost tab = (TabHost) findViewById(android.R.id.tabhost);

		tab.setup();
		TabHost.TabSpec spec;

		spec = tab.newTabSpec("t1");
		spec.setContent(R.id.tab1_private);
		spec.setIndicator("Private Message");
		tab.addTab(spec);
		spec = tab.newTabSpec("t2");
		spec.setContent(R.id.tab2_private);
		spec.setIndicator("New");
		tab.addTab(spec);

		tab.setCurrentTab(0);

		tab.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
			public void onTabChanged(String arg0) {
				String s = "Tab tag =" + arg0 + "; index ="
						+ tab.getCurrentTab();
				Log.d("Tab change : ", s);
			}
		});
	}
}